//
// Created by Ryan on 2020/11/28.
//

#include "types.h"
#include "string.h"

inline void memcpy(void *dest, const void *src, uint32_v len)
{
    // may err
    for (; len > 0; len--)
    {
        *(uint8_v *)dest++ = *(uint8_v *)src++;
    }
}

inline void *memset(void *dest, uint8_v val, uint32_v len)
{
    void *res = dest;
    for (; len > 0; len--)
    {
        *(uint8_v *)dest++ = val;
    }
    return res;
}

inline void bzero(void *dest, uint32_v len)
{
    memset(dest, 0, len);
}

//两个字符串自左向右逐个字符相比（按 ASCII 值大小相比较），直到出现不同的字符或遇 \0 为止
inline int strcmp(const char *str1, const char *str2)
{
    while (*str1)
    {
        if (*str1++ < *str2++)
        {
            return -1;
        }
        else if (*str1++ > *str2++)
        {
            return 1;
        }
    }
    if (!*str2)
    {
        return 1;
    }
    return 0;
}

// 非安全
inline char *strcpy(char *dest, const char *src)
{
    char *res = dest;
    while (*src)
    {
        *dest++ = *src++;
    }
    return res;
}

// 可能是不完全实现
inline char *strcat(char *dest, const char *src)
{
    char *res = dest;
    while (*dest)
    {
        dest++;
    }
    while (*src)
    {
        *dest++ = *src++;
    }
    return res;
}

inline int strlen(const char *src)
{
    // error code
    int len = 0;
    while (*src++)
    {
        len++;
    }
    return len;
}
